﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CreateGUID
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            recordGuid(Create());
        }

        private string Create()
        {
            var newGuid = Guid.NewGuid().ToString();
            txtGuid.Text = newGuid;
            try
            {
                Clipboard.SetDataObject(newGuid);
            }
            catch (Exception ex)
            {
                if (ex.Message == "所请求的剪贴板操作失败。")
                {
                    MessageBox.Show("剪贴板正忙!");
                }
            }
            return newGuid;
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
           recordGuid(Create());
        }

        private void recordGuid(string guid)
        {
            var path = ConfigurationManager.AppSettings["guidRecordPath"];
            var writer = new StreamWriter(path, true);
            writer.WriteLine(guid);
            writer.Close();
        }
    }
}
